package com.example.shop.controller;

import com.example.shop.entity.Orders;
import com.example.shop.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName OrdersController
 * @Description TODO
 * @Author 萍
 * @Date 2025/3/26 10:21
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 通过ID查询单条数据
     *
     * @param orderid 主键
     * @return 实例对象
     */
    @GetMapping("/{orderid}")
    public ResponseEntity<Orders> queryById(@PathVariable("orderid") Integer orderid) {
        Orders orders = ordersService.queryById(orderid);
        if (orders != null) {
            return ResponseEntity.ok(orders);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 新增数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @PostMapping
    public ResponseEntity<Orders> insert(@RequestBody Orders orders) {
        Orders insertedOrders = ordersService.insert(orders);
        return ResponseEntity.ok(insertedOrders);
    }

    /**
     * 修改数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @PutMapping
    public ResponseEntity<Orders> update(@RequestBody Orders orders) {
        Orders updatedOrders = ordersService.update(orders);
        return ResponseEntity.ok(updatedOrders);
    }

    /**
     * 通过主键删除数据
     *
     * @param orderid 主键
     * @return 是否成功
     */
    @DeleteMapping("/{orderid}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("orderid") Integer orderid) {
        boolean isDeleted = ordersService.deleteById(orderid);
        return ResponseEntity.ok(isDeleted);
    }

    /**
     * 查询所有数据
     *
     * @return 实例对象列表
     */
    @GetMapping("/all")
    public ResponseEntity<List<Orders>> queryAll() {
        List<Orders> ordersList = ordersService.queryAll();
        return ResponseEntity.ok(ordersList);
    }
}
